//
//  ZTodoMeetingVC.m
//  WeClubs
//
//  Created by Zed Link on 8/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZTodoMeetingVC.h"
#import "ZTodoMeetingCell.h"
#import "ZTodoMeetingDetailVC.h"
#import "ZTodoFlowLayout.h"

static NSString *MeetingCellReuseID = @"ZMeetingCellReuseID";

@interface ZTodoMeetingVC () <UICollectionViewDelegate, UICollectionViewDataSource>

/** 待办会议列表 */
@property (weak, nonatomic) UICollectionView *meetingView;

/** 待办会议数据源 */
@property (strong, nonatomic) NSArray <ZTodoMeetingModel *> *meetingModels;

@end

@implementation ZTodoMeetingVC

- (void)zLoadSubview
{
    [super zLoadSubview];
    
    // ========== 待办会议列表 ==========
    ZTodoFlowLayout *layout = [[ZTodoFlowLayout alloc] init];
    
    UICollectionView *meetingView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    meetingView.delegate = self;
    meetingView.dataSource = self;
    
    [meetingView setBackgroundColor:GENERAL_VIEW_BG_COLOR];
    [meetingView registerNib:[UINib nibWithNibName:@"ZTodoMeetingCell" bundle:nil] forCellWithReuseIdentifier:MeetingCellReuseID];
    meetingView.contentInset = UIEdgeInsetsMake(DYNAMIC_TOP_SPACE, 0, DYNAMIC_TOP_SPACE, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.meetingView = meetingView;
    [self.view addSubview:meetingView];
    [self.meetingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];    
    NSDictionary *dataPara = @{
                               @"user_id" : @([ZUserInfo sharedInstance].userId),
                               @"token" : [ZUserInfo sharedInstance].token,
                               @"club_id" : @(self.clubModel.clubId ),
                               @"dynamic_type" : @"meeting",
                               @"page_no" : @(0),
                               @"size" : @(8),
                               };

    
    [HTTPRequest POST:kGet_dynamic_listUrl parameter:dataPara success:^(id resposeObject) {
       
        NSArray *mettings = resposeObject[@"data"][@"meeting"];
        
                 NSMutableArray *meetingModels = [NSMutableArray arrayWithCapacity:mettings.count];
                 for (NSDictionary *dict in mettings) {
                     ZTodoMeetingModel *model = [[ZTodoMeetingModel alloc] initWithDictionary:dict];
                     [meetingModels addObject:model];
                 }
                 self.meetingModels = meetingModels;
        
    } failure:^(NSError *error) {
       
    }];
    
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.meetingModels.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZTodoMeetingModel *model = self.meetingModels[indexPath.row];
    
    ZTodoMeetingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MeetingCellReuseID forIndexPath:indexPath];
    cell.model = model;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZTodoMeetingModel *model = self.meetingModels[indexPath.row];
    
    ZTodoMeetingDetailVC *detailVC = [ZTodoMeetingDetailVC zNew];
    detailVC.meetingModel = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}


#pragma mark - Getter & Setter

- (void)setMeetingModels:(NSArray<ZTodoMeetingModel *> *)meetingModels
{
    _meetingModels = meetingModels;
    
    [self.meetingView reloadData];
}

@end
